package com.smartEdu.service.impl.app;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.smartEdu.RongYun.RongYun;
import com.smartEdu.RongYun.RongYun1;
import com.smartEdu.commons.CMap;
import com.smartEdu.commons.LogCvt;
import com.smartEdu.commons.Session;
import com.smartEdu.commons.Tag_Tips;
import com.smartEdu.dao.ExamDao;
import com.smartEdu.dao.HomeworkSubmitDao;
import com.smartEdu.dao.QuestionDao;
import com.smartEdu.exception.JsonException;
import com.smartEdu.exception.ServiceException;
import com.smartEdu.model.ExamPaperModel;
import com.smartEdu.model.HomeworkSingleSubmitModel;
import com.smartEdu.model.HomeworkSubmitModel;
import com.smartEdu.model.QuestionModel;
import com.smartEdu.service.ExamService;
import com.smartEdu.utils.JsonUtil;

@Service(value="examServiceImpl")
public class ExamServiceImpl extends BaseServiceImpl implements ExamService{
	@Autowired
	@Qualifier(value="examDaoImpl")
	private ExamDao examDao;
	
	@Autowired
	@Qualifier(value="questionDaoImpl")
	private QuestionDao questionDao;
	
	@Autowired
	@Qualifier("homeworkSubmitDaoImpl")
	private HomeworkSubmitDao homeworkSubmitDao;

	@SuppressWarnings("unchecked")
	@Override
	public void getExamPaper(Session sess) {
		List<ExamPaperModel> examPaperModels=null;
	    List<HomeworkSubmitModel> homeworkSubitModelmodel=null;
	    List<HomeworkSingleSubmitModel> homeworkSingleSubmitModelList=null;
	    RongYun1 rongyun1=new RongYun1();
		CMap map=null;

		try {
			map=SetToMap(sess);
			HashMap<String, Object> map2=new HashMap<>();
			map2.put("pk", map.gets("homeworkpk"));
			map2.put("sid",map.gets("id"));
			
			examPaperModels=examDao.getExamPaperByPK(map.gets("pk"));
			for (ExamPaperModel models : examPaperModels) {
				List<HashMap<String, Object>> listmaps=(List<HashMap<String, Object>>)models.getQuestionString();
				for (HashMap<String, Object> listmap : listmaps) {
					String questType=listmap.get("type").toString();
					List<QuestionModel> aa=questionDao.getQuestionById(Tag_Tips.QUESBANK+questType, (List<String>)listmap.get("list"));
					homeworkSubitModelmodel=homeworkSubmitDao.getHomeworkCompletedInfo(map2);
				    String strJson=null;
					if(homeworkSubitModelmodel.size()==0){
				    	
				    }else {
				    	strJson=JsonUtil.toJson(homeworkSubitModelmodel.get(0).getCompleted());
				        homeworkSingleSubmitModelList=JsonUtil.toListObject(strJson, HomeworkSingleSubmitModel.class);
				    }
					
					
					System.out.println(strJson);
				    
					for(QuestionModel questionModel:aa){
						questionModel.setStatus("0");
						questionModel.setRightorwrong(2);
						questionModel.setMyanswer("null");
//						if(isTrue(String.valueOf(questionModel.getId()),strJson)){
//							questionModel.setStatus("1");
//						}
						if(!(homeworkSingleSubmitModelList==null)){
						   for(int j=0;j<homeworkSingleSubmitModelList.size();j++){
							if(questionModel.getId()==homeworkSingleSubmitModelList.get(j).getQid()){
								questionModel.setStatus("1");
								questionModel.setMyanswer(homeworkSingleSubmitModelList.get(j).getAnswer());
								questionModel.setRightorwrong(homeworkSingleSubmitModelList.get(j).getRight_wrong());
							    break;
							  }
						   }
						}
					}
					models.setQuestion(aa);
				}
				models.setQuestionString(null);
			}
			sess.put("exams", examPaperModels);
			sess.put(Tag_Tips.RESPCODE, Tag_Tips.OK);
		}
		catch (JsonException e){
			LogCvt.error(Tag_Tips.JSONERROR,e);
			throw new ServiceException("ExamPaper业务流程出错");
		}
		catch (Exception e) {
			LogCvt.error(e.getMessage(),e);
			throw new ServiceException("Exampaper业务流程出错");
		}
		finally {
			map=null;
		}
	}
	
	 public Boolean isTrue(String id,String json){
			System.out.println(id+json);
		    Boolean flag=false;
		     ObjectMapper objectMapper = new ObjectMapper();
			    try {
			        List<LinkedHashMap<String, Object>> list = objectMapper.readValue(json, List.class);
			        for (int i = 0; i < list.size(); i++) {
			            Map<String, Object> map = list.get(i);
			            System.out.println(map.get("qid"));
			             if(map.get("qid").toString().equals(id)){
			                flag=true;
			                break;
			               }
			        }
			    } catch (Exception e) {
			        e.printStackTrace();
			    }
			  return flag;
		  }

}
